/**
 * 日志工具类
 * 提供统一的日志记录功能
 */

class Logger {
  constructor() {
    this.isDevelopment = process.env.NODE_ENV === 'development' || !process.env.NODE_ENV
  }

  /**
   * 信息日志
   */
  info(message, ...args) {
    console.log(`[INFO] ${new Date().toISOString()} - ${message}`, ...args)
  }

  /**
   * 警告日志
   */
  warn(message, ...args) {
    console.warn(`[WARN] ${new Date().toISOString()} - ${message}`, ...args)
  }

  /**
   * 错误日志
   */
  error(message, error = null, ...args) {
    if (error) {
      console.error(`[ERROR] ${new Date().toISOString()} - ${message}`, error.message || error, ...args)
      if (this.isDevelopment && error.stack) {
        console.error('Stack trace:', error.stack)
      }
    } else {
      console.error(`[ERROR] ${new Date().toISOString()} - ${message}`, ...args)
    }
  }

  /**
   * 调试日志（仅在开发环境显示）
   */
  debug(message, ...args) {
    if (this.isDevelopment) {
      console.log(`[DEBUG] ${new Date().toISOString()} - ${message}`, ...args)
    }
  }

  /**
   * WebSocket 连接日志
   */
  connection(message, connId = '', ...args) {
    this.info(`[CONNECTION] ${connId ? `[${connId}] ` : ''}${message}`, ...args)
  }

  /**
   * WebSocket 消息日志
   */
  message(message, connId = '', ...args) {
    this.debug(`[MESSAGE] ${connId ? `[${connId}] ` : ''}${message}`, ...args)
  }

  /**
   * Redis 操作日志
   */
  redis(message, ...args) {
    this.debug(`[REDIS] ${message}`, ...args)
  }

  /**
   * 房间操作日志
   */
  room(message, roomId = '', ...args) {
    this.info(`[ROOM] ${roomId ? `[${roomId}] ` : ''}${message}`, ...args)
  }

  /**
   * 性能日志
   */
  performance(message, duration = 0, ...args) {
    this.debug(`[PERFORMANCE] ${message} (${duration}ms)`, ...args)
  }
}

// 创建单例实例
const logger = new Logger()

export default logger
